package com.fxz.rabbitmq.producer.broker;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @author Fxz
 * @version 1.0
 * @date 2021-08-17 14:31
 */
@Slf4j
public class AsyncBaseQueue {

    private static final Integer THREAD_SIZE = Runtime.getRuntime().availableProcessors();

    private static final Integer QUEUE_SIZE = 10000;

    private static ExecutorService senderAsync =
            new ThreadPoolExecutor(
                    THREAD_SIZE,
                    THREAD_SIZE,
                    60L,
                    TimeUnit.SECONDS,
                    new ArrayBlockingQueue<>(QUEUE_SIZE),
                    new ThreadFactory() {
                        @Override
                        public Thread newThread(Runnable r) {
                            Thread thread = new Thread(r);
                            thread.setName("rabbitmq_client_async_sender");
                            return thread;
                        }
                    },
                    new RejectedExecutionHandler() {
                        @Override
                        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                            log.error("async sender is error reject,runnable:{},executor:{}",r,executor);
                        }
                    });

    public static void submit(Runnable runnable){
        senderAsync.submit(runnable);
    }
}
